package com.souku.finance.application.ui.invite;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.InviteListAdapter;
import com.souku.finance.application.base.BaseFragment;
import com.souku.finance.application.bean.InviteListBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/5.
 */

public class InviteFragment extends BaseFragment {

    protected View rootView;
    protected PullToRefreshListView listView;
    protected InviteListAdapter adapter;
    protected List<InviteListBean.DataBean> list = new ArrayList<>();
    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fra_invite, container, false);
        setContent(rootView);
        initView(rootView);
        adapter = new InviteListAdapter(getActivity(), list);
        listView.setAdapter(adapter);
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), TenderDetailsActivity.class);
                intent.putExtra(Constant.Default, list.get(position - 1).getId());
                startActivity(intent);
            }
        });
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                list.clear();
                page = 1;
                getDeal_list();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getDeal_list();
            }
        });
        listView.setRefreshing(true);
        return rootView;
    }

    @Override
    public void internet() {
        listView.setRefreshing(true);
    }

    @Override
    public void showFragment() {

    }

    private void initView(View rootView) {
        listView = (PullToRefreshListView) rootView.findViewById(R.id.listView);
    }

    private void getDeal_list() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("page", page + "");
        RequestManager.getInstance().post(UrlConstact.DEAL_LIST, ParamUtil.getSing(map), new ReqListener(getActivity(), new SuccessError() {
            @Override
            public void onSuccess(String response) {
                listView.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    InviteListBean bean = JsonUtil.fastBean(response, InviteListBean.class);
                    if (bean.getData().size() == 0) {
                        ToastUtil.show("没有更多了");
                    } else {
                        list.addAll(bean.getData());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                listView.onRefreshComplete();
//                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }
}
